from flask import Blueprint, request, make_response, jsonify, redirect

from app import db
from util.captcha.captcha import captcha
import json, random, time, requests
from util.myredis import r
from models.user import *
from werkzeug.security import generate_password_hash,check_password_hash

from utils.myjwt import myjwt

label_blue = Blueprint("labelblue",__name__)

@label_blue.route('/get_labels')
def get_labels():
    label = Need_Label.query.all()
    lists = []
    list_one = []
    for i in label:
        # 获取所有pid==0的数据
        if i.pid == 0:
            # 判断时候在list_one列表中
            if i.id not in list_one:
                # 将pid==0的数据加入到lists总
                lists.append({'id':i.id,'name':i.name,'son':[]})
                list_one.append([i.id])

    for j in label:
        for index,p in enumerate(lists):
            # 判断所有数据的pid是否等于lists中的id，将等于的数据加入到lists中的son中
            if j.pid == p['id']:
                lists[index]['son'].append({'id':j.id,'tname':j.name,'tow_son':[]})

    for s in label:
        for index,p in enumerate(lists):
            aa = lists[index]['son']
            for indexa,a in enumerate(aa):
                if s.pid == a['id']:
                    aa[indexa]['tow_son'].append({'id':s.id,'sname':s.name})
    return jsonify({
        'code':200,
        'msg':"OK",
        'list':lists,
    })




    # return jsonify({
    #     'code':200,
    #     'msg':"获取一级标签成功",
    #     'data':lists
    # })

@label_blue.route('/add_server')
def get_label2():
    data = json.loads(request.data)
    title = data['title']
    desc = data['desc']
    label = data['label']
    money = data['money']
    user_id = data['user_id']
    if not all([title,desc,label,money,user_id]):
        return jsonify({
            'code':400,
            'msg':"参数不完整"
        })
    
@label_blue.route('/server_index')
def server_index():
    ...

























